<?php
class ArrowCtrlTemplate extends ArrowTagContainer{
	
	private $nativePHP = false;
	private $transport;
	private $hasChildren;
	private $functionName = "";
	
	public function configure(){
		
		$this->addProperties(array(
			"name" => null,
			"context" => false,
			"transport" => false,
			"nativePHP" => false
		));
		
		$this->setContentCodeKept(true);
		$this->setDisplay(false);
		$this->setParseContent(true);
	}
	
	public function init(){
		parent::init();
		$this->nativePHP = $this->getProperty("nativePHP");
		if($this->nativePHP){
			$this->functionName = "templateFunct".$this->getNodeIndex();
		}
		$this->transport = $this->getProperty("transport");
		$children = $this->getChildren();
		$this->hasChildren = !empty($children);
	}
	
	public function getSource(){
		return $this->getContentCode();
	}
	
	public function processTemplate( $context ){
		if($this->nativePHP){
			ob_start();
			call_user_func($this->functionName, $context, $this->transport);
			$result = ob_get_contents();
            ob_end_clean();
			return $result;
		}
		$this->setDisplay(true);
		//TODO Isolate smarty instance
		$instance = ArrowView::getCurrentView()->getTemplateSystem();
		$instance->assign("context", $context);
		$instance->assign("transport", $this->transport);
		if($this->hasChildren)
			$this->initAll();
			
		$compiler = new SmartyCompiler($this->getContentCode(), $instance);
		
		$result = $compiler->getResult(array());
		
		return $result;
	}	
	
	public function getInitInstructions(){
		$str = parent::getInitInstructions();
		$args = $this->getArgs();
		if(isset($args["nativePHP"]) && $args["nativePHP"])
			$str.= "\n function templateFunct".$this->getNodeIndex()."( \$context, \$transport ){ \n ?>\n". $this->getContentCode()."\n<?php \n }";
			
		$str = stripslashes( str_replace("_arrow_end_delim_", "?>", $str));
		return $str;
	}
	
	public function generateBlockStartCode(){
		return "{*";
	}
	
	public function generateBlockEndCode(){
		return "*}";
	}
}
?>
